﻿using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System;
using System.Collections.Generic;
using Uportal.Shp.Core.Const;
using Uportal.Shp.Core.Common;
using Uportal.Shp.Core.Enums;
using Uportal.Shp.Core.Services;
using System.Web.UI;

namespace Uportal.Shp.Init
{
    public class FeedbackIterators : ListFieldIterator
    {
        NLogLogger _logger = new NLogLogger(@"c:\Temp\Logs\Uportal.LFI_${shortdate}.log");

        protected override void CreateChildControls()
        {
            var fieldIterators = new List<ListFieldIterator>();
            switch (SPContext.Current.FormContext.FormMode)
            {
                case SPControlMode.New:
                    fieldIterators = getNewFieldIterators();
                    break;
                case SPControlMode.Edit:
                    fieldIterators = getEditFieldIterators();
                    break;
                case SPControlMode.Display:
                    fieldIterators = getDisplayFieldIterators();
                    break;
                default:
                    //fieldIterators = getDisplayFieldIterators();
                    break;
            }
            foreach(var fieldIterator in fieldIterators)
                this.Controls.Add(fieldIterator);
        }

        private List<ListFieldIterator> getNewFieldIterators()
        {
            return new List<ListFieldIterator>() 
            {
                new ListFieldIterator()
                {
                    ControlMode = SPControlMode.New,
                    ExcludeFields = string.Join(";#", (new string[] { "Title", "answer", "stateOfQuestion", "dateOfAnswer", "isApproved" }))
                }
            };
        }

        private List<ListFieldIterator> getDisplayFieldIterators()
        {
            var state = Convert.ToString(SPContext.Current.ListItem[FieldInfo.Feedback.StateQuestion]);

            return new List<ListFieldIterator>()
            {
                new ListFieldIterator()
                {
                    ControlMode = SPControlMode.Display,
                    ExcludeFields = string.Join(";#", (new string[] { "Title", "stateOfQuestion", "dateOfAnswer", "typeOfQuestion", "isApproved" }))
                }
            };
        }

        private List<ListFieldIterator> getEditFieldIterators()
        {
            var state = Convert.ToString(SPContext.Current.ListItem[FieldInfo.Feedback.StateQuestion]);

            //new
            if (state == EnumResolver.GetStringValue(StateQuestion.ForApproved))
            {
                return new List<ListFieldIterator>()
                {
                    new ListFieldIterator()
                    {
                        ControlMode = SPControlMode.Display,
                        ExcludeFields = string.Join(";#", (new string[] { "Title","typeOfQuestion","dateOfAnswer","stateOfQuestion","answer","whom", "isApproved" }))
                    } 
                    ,new ListFieldIterator()
                    {
                        ControlMode = SPControlMode.Edit,
                        ExcludeFields = string.Join(";#", (new string[] { "Title","question","typeOfQuestion","dateOfAnswer","answer", "isApproved" }))
                                                                          
                    }                                
                };
            }

            //Approved
            if (state == EnumResolver.GetStringValue(StateQuestion.Approved))
            {
                return new List<ListFieldIterator>()
                {
                    new ListFieldIterator()
                    {
                        ControlMode = SPControlMode.Display,
                        ExcludeFields = string.Join(";#", (new string[] { "Title","answer","typeOfQuestion","dateOfAnswer","stateOfQuestion","isApproved" }))
                    }
                    ,new ListFieldIterator()
                    {
                        ControlMode = SPControlMode.Edit,
                        ExcludeFields = string.Join(";#", (new string[] { "Title","question","dateOfAnswer","whom","stateOfQuestion","isApproved" }))
                    }

                };
            }

            //Answer
            if (state == EnumResolver.GetStringValue(StateQuestion.Answered))
            {
                return new List<ListFieldIterator>()
                {
                    new ListFieldIterator()
                    {
                        ControlMode = SPControlMode.Display,
                        ExcludeFields = string.Join(";#", (new string[] { "Title","typeOfQuestion","stateOfQuestion","dateOfAnswer","isApproved" }))
                    }
                };
            }
            
            //closed
            if (state == EnumResolver.GetStringValue(StateQuestion.Closed))
            {
                return new List<ListFieldIterator>()
                {
                    new ListFieldIterator()
                    {
                        ControlMode = SPControlMode.Display,
                        ExcludeFields = string.Join(";#", (new string[] { "Title","answer","typeOfQuestion","dateOfAnswer","whom","isApproved" }))
                    }
                };
            }
            
            // default
            return new List<ListFieldIterator>()
                {
                    new ListFieldIterator()
                    {
                        ControlMode = SPControlMode.Display,
                        ExcludeFields = string.Join(";#", (new string[] { "Title","typeOfQuestion","dateOfAnswer","whom","isApproved" }))
                    }
                };
        }

        protected BaseFieldControl GetFieldControlByName(String fieldNameToFind)
        {
            foreach (Control control in SPContext.Current.FormContext.FieldControlCollection)
            {
                if (control is BaseFieldControl)
                {
                    BaseFieldControl baseField = (BaseFieldControl)control;
                    String fieldName = baseField.FieldName;
                    _logger.Info(fieldName);
                    if ((fieldName == fieldNameToFind) &&
                        (GetIteratorByFieldControl(baseField).ClientID == ClientID))
                    {
                        return baseField;
                    }
                }

            }
            return null;
        }

        protected ListFieldIterator GetIteratorByFieldControl(BaseFieldControl fieldControl)
        {
            return (ListFieldIterator)fieldControl.Parent.Parent.Parent.Parent.Parent;
        }
    }
}
